{% extends 'base.html' %}
{% load staticfiles %}
{% block aside %}
<section class="vbox">
<section class="scrollable wrapper-lg">
  <div class="row">
    <div class="col-sm-8">
    <div class="form-group">
        <a href="{% url 'post:post' %}">
            <button type="submit" class="btn btn-info">回到上一页</button>
        </a>
    </div>
      <div class="panel">
        <div class="wrapper-lg">
          <h2 class="m-t-none text-black">{{ post.title }}</h2>
          <div class="post-sum">
            <p>{{ post.content }}</p>
          </div>
          <div class="line b-b"></div>
            {% if user.is_authenticated %}
              <div class="text-muted">
                <i class="fa fa-user icon-muted"></i> 答主: <a href="{% url 'user:show_another' post.user_id %}" class="m-r-sm">{{ post.user.username }}</a>
                <i class="fa fa-clock-o icon-muted"></i> {{ post.publish_time }}
                <a href="#" class="m-sm"><i class="fa fa-comment-o icon-muted"></i> {{ comment_list.count }} 条回答</a>
              </div>
              <div class="line"></div>
              <a class="btn btn-default btn-xs text-center" id="favorite" href="javascript:void(0)" onclick="add_favorite_post('{{ post.id }}')" data-toggle="class">
                  <i class="fa fa-star-o text-muted text"></i>
                  收藏
              </a>
              <a class="btn btn-default btn-xs text-center" id="like" href="javascript:void(0)" onclick="post_like('{{ post.id }}')" data-toggle="class">
                  <i class="fa fa-thumbs-o-up text-muted text"></i>
                  点赞
              </a>
            {% else %}
              <div class="text-muted">
                <i class="fa fa-user icon-muted"></i> 答主: <a href="javascript:void(0)" onclick="show_toast()" class="m-r-sm">{{ post.user.username }}</a>
                <i class="fa fa-clock-o icon-muted"></i> {{ post.publish_time }}
                <a href="#" class="m-sm"><i class="fa fa-comment-o icon-muted"></i> {{ comment_list.count }} 条回答</a>
              </div>
              <div class="line"></div>
              <a class="btn btn-default btn-xs text-center" id="favorite" href="javascript:void(0)" onclick="show_toast()" data-toggle="class">
                  <i class="fa fa-star-o text-muted text"></i>
                  收藏
              </a>
              <a class="btn btn-default btn-xs text-center" id="like" href="javascript:void(0)" onclick="show_toast()" data-toggle="class">
                  <i class="fa fa-thumbs-o-up text-muted text"></i>
                  点赞
              </a>
            {% endif %}
        </div>
      </div>
      <div class="panel">
        <div class="wrapper-lg">
          <h3 class="m-t-none text-black">全部回答</h3>
          {% for comment in comment_list %}
          <div class="line"></div>
          <div class="post-sum">
            <p>{{ comment.content }}</p>
          </div>
          <div class="text-muted">
          {% if user.is_authenticated %}
            <i class="fa fa-user icon-muted"></i> 回答者: <a href="{% url 'user:show_another' comment.user_id %}" class="m-r-sm">{{ comment.user.username }}</a>
          {% else %}
            <i class="fa fa-user icon-muted"></i> 回答者: <a href="javascript:void(0)" onclick="toast_warning()" class="m-r-sm">{{ comment.user.username }}</a>
          {% endif %}
            <i class="fa fa-clock-o icon-muted"></i> {{ comment.collect_time }}
          </div>
          <div class="line b-b"></div>
          {% endfor %}
        </div>
      </div>
      <h4 class="m-t-lg m-b">发表你的看法吧</h4>
      <form action="{% url 'comment:add_post_comment' post.id %}?next={{ request.path }}" method="post">
      {% csrf_token %}
      <div class="form-group">
          {{ form.content }}
          {{ error }}
      </div>
      <div class="form-group">
      {% if user.is_authenticated %}
          <button type="submit" id="p-reply" class="btn btn-success">提交</button>
      {% else %}
          <button type="button" onclick="toast_warning()" class="btn btn-success">提交</button>
      {% endif %}
      </div>
    </form>
    </div>
    <div class="col-sm-4">
      <div class="panel panel-default">
        <div class="panel-heading">最新主题</div>
        <div class="panel-body">
        {% for recent in recent_list %}
          <article class="media">
            <a href="#" class="pull-left thumb thumb-wrapper m-t-xs">
              <img src="{{ recent.user.avatar.url }}">
            </a>
            <div class="media-body">
              <a href="{{ recent.get_absolute_url }}" class="font-semibold">{{ recent.title }}</a>
              <div class="text-xs block m-t-xs" style="white-space:nowrap;overflow:hidden;text-overflow: ellipsis">{{ recent.summary }}</div>
              <div class="text-xs block m-t-xs"><a href="#">{{ recent.user.username }}</a> 发布于 {{ recent.publish_time }}</div>
            </div>
          </article>
        {% endfor %}
        </div>
      </div>
    </div>
  </div>
</section>
<script type="text/javascript" src="{% static 'js/jquery.min.js' %}"></script>
<script type="text/javascript">
     function add_favorite_post(post_id){
        $.post("{% url 'favorite:add_favorite_post' %}",{
            'post_id': post_id,
            'user_id': '{{ user.id }}',
        },
         function(data) {
            var ele = $("#favorite");
            if (data.status === 'ok') {
                ele.addClass("active");
                ele.children().removeClass("fa-star-o text-muted");
                ele.children().addClass("fa-star text-active text-danger");
                show_toast('success', data.msg, '{{ user.avatar.url }}')
            }
            else {
                show_toast('warning', data.msg, '{{ user.avatar.url }}')
            }
        });
    }
</script>
<script type="text/javascript">
      function post_like(post_id){
          $.post("{% url 'post:post_like' %}",{
              'post_id': post_id,
              'user_id': '{{ user.id }}',
          },
          function(data) {
              {#var like_count = data.like_count;#}
              var ele = $("#like");
              if (data.status === 'ok'){
                  ele.addClass("active");
                  ele.children().removeClass("fa-thumbs-o-up text-muted");
                  ele.children().addClass("fa-thumbs-up text-active text-danger");
                  show_toast('success', '点赞成功!','{{ user.avatar.url }}');
                  {#$('#like').text(like_count.toString() + ' 点赞');#}
              }
              else {
                  show_toast('warning', '不允许重复点赞!','{{ user.avatar.url }}');
                  {#$('#like').text(like_count.toString() + ' 点赞');#}
              }
          });
      }
</script>
<script type="text/javascript">
    $('#p-reply').click(function () {
        show_toast('success', '回复成功!', '{{ user.avatar.url }}')
    })
</script>
{% endblock %}